﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using System.IO;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 插入标题图片命令。
    /// ★①，修改类名。
    /// </summary>
    public static class InsertTitleImageCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static InsertTitleImageCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "InsertTitleImageCommand",
                "InsertTitleImageCommand",
                typeof(InsertTitleImageCommand),//创建RoutedUICommand对象
                new InputGestureCollection()
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.I,ModifierKeys.Control,"Ctrl+I"),
                });//插入标题图片不给命令。

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            //如何插入一个图片？？？

            InsertTitleImage();
        }

        internal static void InsertTitleImage(string imgFileFullPath = null, string linkText = null)
        {
            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                //先取出当前选中的图片索引。
                Title curTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
                TitleImage ti = new TitleImage(Globals.MainWindow, curTitle);

                XmlNode imgSetNode = curTitle.XmlData.SelectSingleNode("ImageSet");
                if (imgSetNode == null)
                {
                    imgSetNode = curTitle.XmlData.AppendXml("<ImageSet/>");
                }

                int index = curTitle.ImgStackPanel.Children.IndexOf(Globals.MainWindow.SelectedTitleImage);

                XmlNode newImgNode = imgSetNode.InsertXmlAt(Properties.Resources.TitleImageOutXml, index);
                if (string.IsNullOrEmpty(linkText) == false)
                {
                    newImgNode.SetAttribute("Link", linkText);
                }
                ti.XmlData = newImgNode;

                ModifingItem mi = new ModifingItem()
                {
                    IsNeedRefreshTitleAndLinesLocation = true,
                    ModifingName = "插入标题图像"
                };

                Action actInsertTitleImage = new Action(ti.ID, index,
                    ti.GetType().Name, "InsertTitleImage",
                    null, ti.XmlData.OuterXml, ActionType.InsertTitleImage, curTitle.ID);

                curTitle.ImgStackPanel.Children.Insert(index, ti);
                mi.Add(actInsertTitleImage);

                mi.OldSelectedTitleID = curTitle.ID;
                mi.NewSelectedTitleID = curTitle.ID;//如果用TitleImage的ID会很难做。
                mi.OldSelectedTitleImageID = Globals.MainWindow.SelectedTitleImage.ID;
                mi.NewSelectedTitleImageID = ti.ID;

                ti.IsSelected = true;
                Globals.MainWindow.SelectedTitleImage = ti;

                if (imgFileFullPath != null)
                {
                    ti.LoadImageFromFile(mi, imgFileFullPath);
                }
                else
                {
                    curTitle.Editor.Do(mi);
                    curTitle.Editor.IsModified = true;
                    curTitle.Editor.RefreshTitleAndLinesLocation();
                }
            }
            else
            {
                if (Globals.SelectedTitlesList.Count <= 0) return;

                //这种情况，添加到末尾好了。

                Title curTitle = null;

                foreach (Title t in Globals.SelectedTitlesList)
                {
                    if (t.IsMainSelectedTitle)
                    {
                        curTitle = t;
                        break;
                    }
                }

                if (curTitle == null)
                {
                    MessageBox.Show("　　发生意外，找不到插入标题图片的基准位置。\r\n" +
                        "　　请重新选取一个标题或标题图片作为插入新标题图片的基准位置！",
                        Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                TitleImage ti = new TitleImage(Globals.MainWindow, curTitle);

                XmlNode imgSetNode = curTitle.XmlData.SelectSingleNode("ImageSet");
                if (imgSetNode == null)
                {
                    imgSetNode = curTitle.XmlData.AppendXml("<ImageSet/>");
                }

                XmlNode newImgNode = imgSetNode.AppendXml(Properties.Resources.TitleImageOutXml);
                if (string.IsNullOrEmpty(linkText) == false)
                {
                    newImgNode.SetAttribute("Link", linkText);
                }
                ti.XmlData = newImgNode;

                ModifingItem mi = new ModifingItem()
                {
                    IsNeedRefreshTitleAndLinesLocation = true,
                    ModifingName = "插入标题图像"
                };

                Action actInsertTitleImage = new Action(ti.ID,
                    curTitle.ImgStackPanel.Children.Count, ti.GetType().Name, "InsertTitleImage",
                    null, ti.XmlData.OuterXml, ActionType.InsertTitleImage, curTitle.ID);

                curTitle.ImgStackPanel.Children.Add(ti);
                mi.Add(actInsertTitleImage);
                mi.OldSelectedTitleID = mi.NewSelectedTitleID = curTitle.ID;
                mi.NewSelectedTitleImageID = ti.ID;

                ti.IsSelected = true;
                Globals.MainWindow.SelectedTitleImage = ti;

                if (imgFileFullPath != null)
                {
                    ti.LoadImageFromFile(mi, imgFileFullPath);
                }
                else
                {
                    curTitle.Editor.Do(mi);
                    curTitle.Editor.IsModified = true;
                    curTitle.Editor.RefreshTitleAndLinesLocation();
                }
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = (Globals.SelectedTitlesList.Count == 1 ||
                Globals.MainWindow.SelectedTitleImage != null);
        }
    }
}
